package com.button.boot.active.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.button.boot.active.model.User;
import com.button.boot.active.service.ActiveMqService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;

@Api("ActiveMq相关的api")
@RestController
@RequestMapping(value = "/activemq")
public class ActiveMqController {
    private static final Logger log = LoggerFactory.getLogger(ActiveMqController.class);
    @Autowired
    private ActiveMqService activeMqService;
    
    @GetMapping(value = "/sendQueueMsg")
    public void sendQueueMsg(@RequestParam(value = "msg", required = true) String msg) {
        try {
            log.info("开始使用sendQueueMsg发送消息. msg={}", msg);
            activeMqService.sendQueueMsg(msg);
            log.info("使用sendQueueMsg发送消息完成. msg={}", msg);
        } catch (Exception e) {
            log.error("使用sendQueueMsg发送消息时出现异常. e={}", e);
        }
    }
    @PostMapping(value = "/sendQueueUserMsg")
    public void sendQueueUserMsg(@ApiParam("同步请求实体对象") User user) {
        try {
            log.info("开始使用sendQueueUserMsg发送消息. user={}", JSONObject.fromObject(user));
            activeMqService.sendQueueMsg(user);
            log.info("使用sendQueueUserMsg发送消息完成. user={}", JSONObject.fromObject(user));
        } catch (Exception e) {
            log.error("使用sendQueueUserMsg发送消息时出现异常. e={}", e);
        }
    }
    @PostMapping(value = "/send2Return")
    public void send2Return(@RequestParam(value = "msg", required = true) String msg) {
    	try {
            log.info("开始使用send2Return发送消息. msg={}", msg);
            activeMqService.send2Return(msg);
            log.info("使用send2Return发送消息完成. msg={}", msg);
        } catch (Exception e) {
            log.error("使用send2Return发送消息时出现异常. e={}", e);
        }
    }

    @PostMapping(value = "/sendACKQueueMsg")
    public void sendACKQueueMsg(String msg) {
        try {
            log.info("开始使用sendACKQueueMsg发送消息. msg={}", msg);
            activeMqService.sendACKQueueMsg(msg);
            log.info("使用sendACKQueueMsg发送消息完成. msg={}", msg);
        } catch (Exception e) {
            log.error("使用sendACKQueueMsg发送消息时出现异常. e={}", e);
        }
    }

    @PostMapping(value = "/sendTopicMsg")
    public void sendTopicMsg(@RequestParam(value = "msg", required = true) String msg) {
        try {
            log.info("开始使用sendTopicMsg发送消息. msg={}", msg);
            activeMqService.sendTopicMsg(msg);
            log.info("使用sendTopicMsg发送消息完成. msg={}", msg);
        } catch (Exception e) {
            log.error("使用sendTopicMsg发送消息时出现异常. e={}", e);
        }
    }

    @PostMapping(value = "/sendTopicUserMsg")
    public void sendTopicUserMsg(@ApiParam("同步请求实体对象") User user) {
        try {
            log.info("开始使用sendTopicUserMsg发送消息. user={}", JSONObject.fromObject(user));
            activeMqService.sendTopicMsg(user);
            log.info("使用sendTopicUserMsg发送消息完成. user={}", JSONObject.fromObject(user));
        } catch (Exception e) {
            log.error("使用sendTopicUserMsg发送消息时出现异常. e={}", e);
        }
    }
    /**
     * 使用延时队列需要在activemq.xml中的<broker></broker>标签里添加schedulerSupport="true"，如下所示：
     * <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true"></broker>
     * @return
     */
    @PostMapping(value = "/sendDelayTopicMsg")
    public void sendDelayTopicMsg(@RequestParam(value = "msg", required = true) String msg) {
    	try {
            log.info("开始使用sendDelayTopicMsg发送消息. msg={}", msg);
            activeMqService.sendDelayTopicMsg(msg);
            log.info("使用sendDelayTopicMsg发送消息完成. msg={}", msg);
        } catch (Exception e) {
            log.error("使用sendDelayTopicMsg发送消息时出现异常. e={}", e);
        }
    }

    /**
     * 使用延时队列需要在activemq.xml中的<broker></broker>标签里添加schedulerSupport="true"，如下：
     * @return
     */
    @PostMapping(value = "/sendDelayTopicTimeMsg")
    public void sendDelayTopicTimeMsg(String msg, long time) {
    	try {
            log.info("开始使用sendDelayTopicTimeMsg发送消息. msg={}, time={}", msg, time);
            activeMqService.sendDelayTopicMsg(msg, time);
            log.info("使用sendDelayTopicTimeMsg发送消息完成. msg={}, time={}", msg, time);
        } catch (Exception e) {
            log.error("使用sendDelayTopicTimeMsg发送消息时出现异常. msg={}, time={}, e={}", msg, time, e);
        }
    }
}
